//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/.
//          VisualNovelToolkit           /_/_/_/_/_/_/_/_/_/.
// Copyright ©2013 - Sol-tribe. /_/_/_/_/_/_/_/_/_/.
//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/.

using UnityEditor;
using UnityEngine;
using System.IO;

namespace ViNoToolkit{

    public class ADVCreateWizard : EditorWindow {
        public string projectFolderName = "New ADV Project";
        public string firstScenarioName = "1";
        public Texture2D titleBGTexture;
		
		private string scenePath = "";

        [MenuItem ("ViNo/Create ADV")]
        static void CreateWizard () {
            GetWindow( typeof(ADVCreateWizard) );
        }

        void OnGUI (){

            EditorGUILayout.BeginHorizontal();
            {
                titleBGTexture = EditorGUILayout.ObjectField( titleBGTexture , typeof(Texture2D) , false , GUILayout.Width( 128f ) , GUILayout.Height( 128f ) ) as Texture2D ;
    
                EditorGUILayout.BeginVertical();
                {
                    EditorGUILayout.LabelField( "Title BG");
                    projectFolderName = EditorGUILayout.TextField( "Project Folder Name" , projectFolderName );
                    firstScenarioName = EditorGUILayout.TextField( "First Scenario Name" , firstScenarioName );
                }
                EditorGUILayout.EndVertical();
            }
            EditorGUILayout.EndHorizontal();

            EditorGUILayout.HelpBox( "Project folder is created under Assets folder." , MessageType.Info );

            EditorGUILayout.BeginHorizontal();
            {
                GUILayout.Space( 30f );
                if( GUILayout.Button( "Create" ) ){
                    OnWizardCreate();
                }
            }
            EditorGUILayout.EndHorizontal();
        }

        void OnWizardCreate () {
			bool succeeded =EditorApplication.SaveScene (scenePath, true);
			if ( ! succeeded) {
				EditorUtility.DisplayDialog ("Note", "Please save scene at first", "OK");
				return;
			}

            // -------------------------------------------.
            //  Create  ADV scene from Templates.
            // -------------------------------------------.
            DrawTemplatesTab.CreateAdvScene( "ADV Scene" , projectFolderName );

            // -------------------------------------------.
            //  Create  folders.
            // -------------------------------------------.
            CreateFolders();

            // -------------------------------------------.
            //  Create  Scenario Script.
            // -------------------------------------------.
            CreateScenario();

            // -------------------------------------------.
            // Change Title BG.
            // -------------------------------------------.
            ChangeTitleBGMaterial();

			// Close this window.
			Close ();
        }  

        void CreateFolders(){
            // Create Resources folder.
            if( ! System.IO.Directory.Exists( "Assets/" + projectFolderName + "/Resources" ) ) {
                 AssetDatabase.CreateFolder( "Assets/" + projectFolderName , "/Resources" );
             }

             // Create ScenarioScripts folder.
            if( ! System.IO.Directory.Exists( "Assets/" + projectFolderName + "/ScenarioScripts" ) ) {
                 AssetDatabase.CreateFolder( "Assets/" + projectFolderName , "ScenarioScripts" );
             }

             // Create Materials folder.
            if( ! System.IO.Directory.Exists( "Assets/" + projectFolderName + "/Materials" ) ) {
                 AssetDatabase.CreateFolder( "Assets/" + projectFolderName , "Materials" );
             }

             // Create GameInfo folder.
            if( ! System.IO.Directory.Exists( "Assets/" + projectFolderName + "/GameInfo" ) ) {
                 AssetDatabase.CreateFolder( "Assets/" + projectFolderName , "GameInfo" );
             }

         }

        void CreateScenario(){
            // Create Scenario Object.
            string filePath = Application.dataPath + "/" + projectFolderName + "/ScenarioScripts/" + firstScenarioName + ".txt";
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.Append( "*start" );
            sb.Append( System.Environment.NewLine );
            sb.Append( "[current layer=TextBox]" );
            sb.Append( System.Environment.NewLine );           
            sb.Append( "[enterscene name=Scene1 fade=true]" );
            sb.Append( System.Environment.NewLine );           
            sb.Append( "[enteractor name=Sachi position=left fade=true]" );
            sb.Append( System.Environment.NewLine );           
            sb.Append( "[enteractor name=Yoshino position=right fade=true]" );
            sb.Append( System.Environment.NewLine );           
            sb.Append( "Hello , World ![p]" );

            File.WriteAllText( filePath , sb.ToString() );
            AssetDatabase.Refresh();

            TextAsset script = AssetDatabase.LoadAssetAtPath( "Assets/" + projectFolderName + "/ScenarioScripts/" + firstScenarioName + ".txt" , typeof(TextAsset) ) as TextAsset;

            GameObject scenarioObj = ViNoToolUtil.CreateANewScenario( firstScenarioName , true , script );

            // -------------------------------------------.
            // Create New Game Info ,  Flag and SaveInfo.
            // -------------------------------------------.
            // Create New Game Info in Project view.
            string newGameInfoPath = "Assets/" + projectFolderName + "/GameInfo/NewGameInfo.asset";    
            ViNoSaveInfo newGameInfo = ScriptableObjectUtility.CreateScriptableObject( "ViNoSaveInfo" , newGameInfoPath ) as ViNoSaveInfo;              

            string[] paths = EditorApplication.currentScene.Split(char.Parse("/"));
            string levelName = paths[paths.Length-1].Split( "."[0] )[0];    

            newGameInfo.m_LoadedLevelName = levelName;
            newGameInfo.m_LoadedLevelIndex = 0;
            newGameInfo.m_CurrentScenarioName = firstScenarioName;

            // Create Flag and SaveInfo.
            string scenarioFlagDataPath = "Assets/" + projectFolderName + "/GameInfo/FlagTable.asset";
            string scenarioSaveDataPath = "Assets/" + projectFolderName + "/GameInfo/SaveData.asset";    

            // Create Game Info.
            ViNoSaveInfo saveInfo = ScriptableObjectUtility.CreateScriptableObject( "ViNoSaveInfo" , scenarioSaveDataPath ) as ViNoSaveInfo;                
            FlagTable flagTable = ScriptableObjectUtility.CreateScriptableObject( "FlagTable" , scenarioFlagDataPath ) as FlagTable;

            ScenarioNode scenarioNode = scenarioObj.GetComponent<ScenarioNode>();       
            scenarioNode.m_PlayAtStart = true;
            scenarioNode.flagTable = flagTable;

            ViNoEditorUtil.CreatePrefab( scenarioObj , "Assets/" + projectFolderName + "/Resources/" + firstScenarioName + ".prefab" );

            ScenarioCtrl scenarioCtrl = GameObject.FindObjectOfType( typeof(ScenarioCtrl) ) as ScenarioCtrl;
            scenarioCtrl.newGameInfo = newGameInfo;
            scenarioCtrl.quickSaveSlot = saveInfo;
            scenarioCtrl.saveInfo = saveInfo;

            GameObject.DestroyImmediate( scenarioObj );
        }

        void ChangeTitleBGMaterial(){
            Material material = new Material (Shader.Find( "Unlit/UnlitAlphaWithFade" ));
            material.mainTexture = titleBGTexture;

            AssetDatabase.CreateAsset(material , "Assets/" + projectFolderName + "/Materials/Title.mat");                  

            GameObject titleBG = GameObject.Find( "Panel_Title/Background");
            MeshRenderer ren = titleBG.GetComponent<MeshRenderer>();
            ren.sharedMaterial = material;
        }

    }
}